package com.datamation.swadeshi.view;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;

import com.datamation.swadeshi.R;
import com.datamation.swadeshi.adapter.InvDetAdapter;
import com.datamation.swadeshi.adapter.OrderDetailsAdapter;
import com.datamation.swadeshi.adapter.ProductAdapter;
import com.datamation.swadeshi.adapter.SetTitleAdapter;
import com.datamation.swadeshi.control.data.DebtorDS;
import com.datamation.swadeshi.control.data.InvDetDS;
import com.datamation.swadeshi.control.data.InvHedDS;
import com.datamation.swadeshi.control.data.ItemPriDS;
import com.datamation.swadeshi.control.data.ItemsDS;
import com.datamation.swadeshi.control.data.OrdDetDS;
import com.datamation.swadeshi.control.data.OrdHedDS;
import com.datamation.swadeshi.control.data.ReasonDS;
import com.datamation.swadeshi.control.data.SalRepDS;
import com.datamation.swadeshi.model.InvDet;
import com.datamation.swadeshi.model.InvHed;
import com.datamation.swadeshi.model.Items;
import com.datamation.swadeshi.model.OrdDet;
import com.datamation.swadeshi.model.OrdHed;
import com.datamation.swadeshi.control.ReferenceNum;
import com.datamation.swadeshi.control.Toast;
public class FragmentSalesManagementVanSalesOthers extends Fragment implements OnClickListener{
	
	View view;
	Toast  toast;
	Spinner spTranType,spReason;
	Button itemSearch, btnAdd;
	EditText et_item,et_unit,et_price,et_cases,et_pieces;
	TableRow reasonTableRow;
	TextView tv_value;
	Items items=null;
	
	//calculation variables
	int totPieces =0, seqno=0, index=0;
	double values=0.00;
	
	ListView lv_orddet_other;
	ArrayList<InvDet> orderList;
	
	ReferenceNum referenceNum;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		view = inflater.inflate(R.layout.sales_management_van_sales_other, container, false);
		
		toast = new Toast();
		
		spTranType= (Spinner) view.findViewById(R.id.sp_trans_type);
		spReason=(Spinner) view.findViewById(R.id.sp_reason);
		
		itemSearch = (Button) view.findViewById(R.id.btn_other_item_search);
		btnAdd=(Button) view.findViewById(R.id.btn_add);
		lv_orddet_other=(ListView) view.findViewById(R.id.lv_orddet_other);
		
		itemSearch.setOnClickListener(this);
		btnAdd.setOnClickListener(this);
		
		et_item=(EditText) view.findViewById(R.id.et_other_item);
		et_unit=(EditText) view.findViewById(R.id.et_other_unit);
		et_price=(EditText) view.findViewById(R.id.et_other_price);
		et_cases=(EditText) view.findViewById(R.id.et_other_cases);
		et_pieces=(EditText) view.findViewById(R.id.et_other_pieces);
		
		reasonTableRow=(TableRow) view.findViewById(R.id.table_row_reason);
		
		tv_value=(TextView) view.findViewById(R.id.tv_other_value);
		
		//transation type spinner realated method
		setTransType();
		
		seqno=0; 
		//index=0;
		
		MainActivity activity =(MainActivity) getActivity();
		
		referenceNum=new  ReferenceNum(getActivity());
		
		InvHedDS invHedDS =new InvHedDS(getActivity());
		ArrayList<InvHed> getInvHed = invHedDS.getAllActiveInvhed(referenceNum.getCurrentRefNo(getResources().getString(R.string.VanNumVal)));
		
		if(!getInvHed.isEmpty()){
			
			for (InvHed invHed : getInvHed) {
								
				activity.selectedInvHed=invHed;
				
				if(activity.selectedDebtor==null){
					DebtorDS debtorDS =new DebtorDS(getActivity());
					activity.selectedDebtor=debtorDS.getSelectedCustomerByCode(invHed.getFINVHED_DEB_CODE());
				}
			}
			
		}
		
		try{
			
			if(activity.selectedDebtor == null){
				//Toast.makeText(getActivity(),"Please select a customer", Toast.LENGTH_SHORT).show();
				toast.createToastMessage("Please select a customer", getActivity());
				getActivity().getActionBar().setSelectedNavigationItem(0);
			}else{
				if(activity.selectedInvHed == null){
					getActivity().getActionBar().setSelectedNavigationItem(1);
				}
			}
		}catch (NumberFormatException e){
			getActivity().getActionBar().setSelectedNavigationItem(0);
			e.printStackTrace();
		}catch (Exception e){
			e.printStackTrace();
		}
		
		fatchDataOthers();
		
		et_price.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				casesCalc();
			}
			public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
			public void afterTextChanged(Editable s) {}
		});
		
		//cases
		et_cases.addTextChangedListener(new TextWatcher(){
		    public void afterTextChanged(Editable s) {
		      	casesCalc();
		    }
		    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
		    public void onTextChanged(CharSequence s, int start, int before, int count){}
		}); 
		
		//Pieces
		et_pieces.addTextChangedListener(new TextWatcher(){
			public void afterTextChanged(Editable s) {
				piecesCalc();
		    }
		    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
		    public void onTextChanged(CharSequence s, int start, int before, int count){}
		}); 
		
		lv_orddet_other.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				InvDet invDet = orderList.get(position);
				
				deleteOrderDialog(getActivity(), "Van Order Details(SA)",invDet.getFINVDET_ID());
				
				return false;
			}
		});
		
		lv_orddet_other.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view2,int position, long id) {
				InvDet invDet = orderList.get(position);
				ItemsDS ds =new ItemsDS(getActivity());
				
				items=new Items();
				items.setFITEM_ITEM_CODE(invDet.getFINVDET_ITEM_CODE());
				
				index= Integer.parseInt(invDet.getFINVDET_ID());
				btnAdd.setText("Edit");
				
				if(invDet.getFINVDET_TYPE().equals("MR")){
					
					ArrayAdapter myAdap = (ArrayAdapter) spTranType.getAdapter();
					int spinnerPosition = myAdap.getPosition("MR - Market Return");
					spTranType.setSelection(spinnerPosition);
					
					ReasonDS reasonDS=new ReasonDS(getActivity());
					ArrayAdapter myAdapReason = (ArrayAdapter) spReason.getAdapter();
					int spinnerPosReason = myAdapReason.getPosition(reasonDS.getReasonByReaCode(invDet.getFINVDET_REA_CODE()));
					spReason.setSelection(spinnerPosReason);
					
					
				}else if(invDet.getFINVDET_TYPE().equals("UR")){
					
					ArrayAdapter myAdap = (ArrayAdapter) spTranType.getAdapter();
					int spinnerPosition = myAdap.getPosition("UR - Re Usable Return");
					spTranType.setSelection(spinnerPosition);
					
				}else if(invDet.getFINVDET_TYPE().equals("FD")){
					
					ArrayAdapter myAdap = (ArrayAdapter) spTranType.getAdapter();
					int spinnerPosition = myAdap.getPosition("FD - Free Issues");
					spTranType.setSelection(spinnerPosition);
					
				}
				
				et_item.setText(ds.getItemNameByCode(invDet.getFINVDET_ITEM_CODE()));
				et_unit.setText(ds.getUnitByCode(invDet.getFINVDET_ITEM_CODE()));
				et_price.setText(invDet.getFINVDET_B_SELL_PRICE());
				et_cases.setText(invDet.getFINVDET_CASE_QTY());
				et_pieces.setText(invDet.getFINVDET_PICE_QTY());
				
			}
		});
		//Highlight text when edittext
				//Cases
				et_cases.setOnFocusChangeListener(new OnFocusChangeListener() {
					
					@Override
					public void onFocusChange(View v, boolean hasFocus) {
						// TODO Auto-generated method stub
						et_cases.setSelectAllOnFocus(true);
					}
				});
				
				//Pieces
				et_pieces.setOnFocusChangeListener(new OnFocusChangeListener()
				{
					@Override
					public void onFocusChange(View v, boolean hasFocus)
					{
						
						
							et_pieces.setSelectAllOnFocus(true);
						
					}
				});
				
				//dis amount
				et_price.setOnFocusChangeListener(new OnFocusChangeListener()
				{
					@Override
					public void onFocusChange(View v, boolean hasFocus)
					{
						
						
						et_price.setSelectAllOnFocus(true);
					
					}
				});
				
			
	return view;	
}
	
	private void deleteOrderDialog(final Context context,String title, final String recordId){

		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
		//alertDialogBuilder.setTitle(title);
		alertDialogBuilder.setMessage("Are you sure you want to delete this entry?");
		alertDialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
		alertDialogBuilder
				.setCancelable(false)
				.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
							
								int count =new InvDetDS(context).deleteInvDetByID(recordId);					
								if(count >0){
									//android.widget.Toast.makeText(context, "Deleted successfully",android.widget.Toast.LENGTH_LONG).show();
									toast.createToastMessage("Deleted successfully", getActivity());
									fatchDataOthers();
									clearTextFields();
								}
							}
						})
				.setNegativeButton("No",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,	int id) {
								dialog.cancel();
								
								
							}
						});
		
		AlertDialog alertD = alertDialogBuilder.create();

		alertD.show();
	}
	
	private void setTransType() {
		// TODO Auto-generated method stub
		List<String> list = new ArrayList<String>();
		list.add("MR - Market Return");
		list.add("UR - Re Usable Return");
		list.add("FR - Free Issue Returns");
		list.add("FD - Free Issues");
		
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, list);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spTranType.setAdapter(dataAdapter);
		
		spTranType.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
		        String currentRoute = spTranType.getSelectedItem().toString();
		       
		        //toast.createToastMessage(currentRoute, getActivity());
		        
		    	//set title 
				setTitle(currentRoute.replaceAll("-", "")+" - Van");
		        //clear method
		        //clearTextFields();
		        
		        if(currentRoute.equals("MR - Market Return")){
		        	
		        	reasonTableRow.setVisibility(View.VISIBLE);
		        	ReasonDS reasonDS =new ReasonDS(getActivity());
		        	List<String> list = reasonDS.getReasonName() ;

		    		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, list);
		    		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		    		spReason.setAdapter(dataAdapter);
		    		
		    		et_price.setEnabled(true);
		    		
		        }else if(currentRoute.equals("UR - Re Usable Return")){
		        	
		        	reasonTableRow.setVisibility(View.GONE);
		    		et_price.setEnabled(true);
		    		
		        }else{
		        	reasonTableRow.setVisibility(View.GONE);
		        	et_price.setEnabled(false);
		        }
		       
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		});
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_other_item_search:
			
			clearTextFields();
			prodcutDetailsDialogbox();
			
			break;
		case R.id.btn_add:
			
			if(!et_item.getText().toString().equals("")){	
				if(values > 0.0 && totPieces >0 ){
					
					//det
					InvDet invDet=new InvDet();
					InvDetDS invDetDS=new InvDetDS(getActivity());
					ArrayList< InvDet> ordList=new ArrayList<InvDet>();
					//hed
					InvHedDS invHedDS =new  InvHedDS(getActivity());
					SalRepDS repDS=new SalRepDS(getActivity());
					ArrayList<InvHed> ordHedList=new ArrayList<InvHed>();
					
					ItemPriDS priDS=new ItemPriDS(getActivity());
					
					MainActivity activity =(MainActivity) getActivity();
					//head
					ordHedList.add(activity.selectedInvHed);
				
					if(invHedDS.createOrUpdateInvHed(ordHedList)>0){
						//details 
						seqno++;
						
						invDet.setFINVDET_ID(index+"");
						invDet.setFINVDET_AMT(String.format("%,.2f",values));
						invDet.setFINVDET_BAL_QTY(totPieces+"");
						invDet.setFINVDET_B_AMT(String.format("%,.2f",values));
						invDet.setFINVDET_B_DIS_AMT("0");
						invDet.setFINVDET_BP_DIS_AMT("0");
						invDet.setFINVDET_B_SELL_PRICE(et_price.getText().toString());
						invDet.setFINVDET_BT_TAX_AMT("0");
						invDet.setFINVDET_BT_SELL_PRICE("0");
						invDet.setFINVDET_CASE((new Double(totPieces/Integer.parseInt(et_unit.getText().toString()))).intValue()+"");
						invDet.setFINVDET_CASE_QTY((new Double(totPieces/Integer.parseInt(et_unit.getText().toString()))).intValue()+"");
						invDet.setFINVDET_DIS_AMT("0");
						invDet.setFINVDET_DIS_PER("0");
						invDet.setFINVDET_FREE_QTY("0");
						invDet.setFINVDET_ITEM_CODE(items.getFITEM_ITEM_CODE());
						invDet.setFINVDET_P_DIS_AMT("0");
						invDet.setFINVDET_PRIL_CODE(priDS.getPrilCodeByItemCode(items.getFITEM_ITEM_CODE()));
						invDet.setFINVDET_QTY(totPieces+"");
						invDet.setFINVDET_DIS_VAL_AMT("0");
						invDet.setFINVDET_PICE_QTY(totPieces % Integer.parseInt(et_unit.getText().toString())+"");
						
						String currentRoute = spTranType.getSelectedItem().toString();
						ReasonDS reasonDS=new ReasonDS(getActivity());

						if(currentRoute.equals("MR - Market Return")){
								
							invDet.setFINVDET_TYPE("MR");
							invDet.setFINVDET_TXN_TYPE("31");
							invDet.setFINVDET_REA_CODE(reasonDS.getReaCodeByName(spReason.getSelectedItem().toString()));
								
						}else if(currentRoute.equals("UR - Re Usable Return")){
								
							invDet.setFINVDET_TYPE("UR");
							invDet.setFINVDET_TXN_TYPE("25");
							invDet.setFINVDET_REA_CODE("");
								
						}else if(currentRoute.equals("FD - Free Issues")){
								
							invDet.setFINVDET_TYPE("FD");
							invDet.setFINVDET_TXN_TYPE("27");
							invDet.setFINVDET_REA_CODE("");
								
						}else if(currentRoute.equals("FR - Free Issue Returns")){
							
							invDet.setFINVDET_TYPE("FR");
							invDet.setFINVDET_TXN_TYPE("35");
							invDet.setFINVDET_REA_CODE("");
							
						}
						
						invDet.setFINVDET_RECORD_ID("");
						invDet.setFINVDET_REFNO(activity.selectedInvHed.getFINVHED_REFNO());
						invDet.setFINVDET_SELL_PRICE(et_price.getText().toString());
						invDet.setFINVDET_SEQNO(seqno+"");
						invDet.setFINVDET_TAX_AMT("0.00");
						invDet.setFINVDET_TAX_COM_CODE("");
						invDet.setFINVDET_TIMESTAMP_COLUMN("");
						invDet.setFINVDET_T_SELL_PRICE("0");
						
						DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
						Date date = new Date();
						invDet.setFINVDET_TXN_DATE(dateFormat.format(date));
					
						invDet.setFINVDET_IS_ACTIVE("1");
						
						ordList.add(invDet);
						
						if(invDetDS.createOrUpdateInvDet(ordList)>0){
							
							clearTextFields();
							toast.createToastMessage("Added successfully", getActivity());
							fatchDataOthers();
							
						}
				}		
					
			}
		}	
						
			break;
		default:
			break;
		}
	}

	
	public void fatchDataOthers(){
		try {
			MainActivity activity =(MainActivity) getActivity();
			lv_orddet_other = (ListView) view.findViewById(R.id.lv_orddet_other);
			lv_orddet_other.clearTextFilter();
			InvDetDS detDS = new InvDetDS(getActivity());
			orderList = detDS.getAllActiveVanDet(activity.selectedInvHed.getFINVHED_REFNO());
			lv_orddet_other.setAdapter(new InvDetAdapter(getActivity(), orderList));
		} catch (Exception e) {
			Log.v("Van detail Others", e.toString());
		}
	}
	//----------Cases Calcuation -----------
	private void casesCalc(){
    	
		if(!(et_unit.length() == 0)){	
		    
    		if(!(et_cases.length() == 0)){
    		
    			if(et_pieces.length() == 0){
    				et_pieces.setText("0");
    			}
    		
    			totPieces=(Integer.parseInt(et_cases.getText().toString()) * Integer.parseInt(et_unit.getText().toString())) +  Integer.parseInt(et_pieces.getText().toString());
    		
    			Log.v("Total Pieces (cases)", totPieces+"");
    		}else{
	    		
    			if(et_pieces.length() == 0){
		    		et_pieces.setText("0");
	    		}
    		
    			totPieces=( 0 * Integer.parseInt(et_unit.getText().toString())) +  Integer.parseInt(et_pieces.getText().toString());
    		
    			Log.v("Total Pieces (cases)", totPieces+"");
    		}
    		
    		if(et_price.length() == 0){
    			et_price.setText("0");
			}
    		values= (Double.parseDouble(et_price.getText().toString())* ((double) totPieces)); 
    		
    		setValues(String.format("%,.2f",values));
   	}
}
//------------- Pieces Calcuation ----------
	private void piecesCalc(){
	    if(!(et_unit.length() == 0)){	
			
	    	if(!(et_pieces.length() == 0)){
		
	    			if(et_cases.length() == 0){
		    			et_cases.setText("0");
		    		}
		    		
	    			totPieces=(Integer.parseInt(et_cases.getText().toString()) * Integer.parseInt(et_unit.getText().toString())) +  Integer.parseInt(et_pieces.getText().toString());
		    		
	    			Log.v("Total Pieces (Pieces)", totPieces+"");
	    		}else{
	    			
	    			if(et_cases.length() == 0){
		    			et_cases.setText("0");
		    		}
		    		
	    			totPieces=(Integer.parseInt(et_cases.getText().toString()) * Integer.parseInt(et_unit.getText().toString())) + 0;
		    		
	    			Log.v("Total Pieces (Pieces)", totPieces+"");
	    		}
	    	
		    	if(et_price.length() == 0){
	    			et_price.setText("0");
				}
	    		values= (Double.parseDouble(et_price.getText().toString())* ((double) totPieces)); 
	    		setValues(String.format("%,.2f", values));
	    	}	
	}
	
	private void setValues(String val){
		tv_value.setText(val);
		
	}
	
	ArrayList<Items> ArrayList = null;
	public void prodcutDetailsDialogbox()
	{		
		final Dialog dialog = new Dialog(getActivity());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.order_details_search_item);
		
		final SearchView search = (SearchView) dialog.findViewById(R.id.et_search);
		final ListView productList=(ListView) dialog.findViewById(R.id.lv_product_list); 
		
		
		dialog.setCancelable(true);
		
		final ItemsDS itemsDS =new ItemsDS(getActivity());
		final ItemPriDS priDS=new ItemPriDS(getActivity());
		productList.clearTextFilter();
		String currentRoute = spTranType.getSelectedItem().toString();
		
		referenceNum=new  ReferenceNum(getActivity());
		if(currentRoute.equals("MR - Market Return")){
			ArrayList= itemsDS.getAllItemForVanSale("","txntype ='31'",referenceNum.getCurrentRefNo(getResources().getString(R.string.VanNumVal)),new SalRepDS(getActivity()).getCurrentRepCode());
		}
		else if(currentRoute.equals("UR - Re Usable Return")){
			ArrayList= itemsDS.getAllItemForVanSale("","txntype ='25'",referenceNum.getCurrentRefNo(getResources().getString(R.string.VanNumVal)),new SalRepDS(getActivity()).getCurrentRepCode());
		}
		else if(currentRoute.equals("FD - Free Issues")){
			ArrayList= itemsDS.getAllItemForVanSale("","txntype ='27'",referenceNum.getCurrentRefNo(getResources().getString(R.string.VanNumVal)),new SalRepDS(getActivity()).getCurrentRepCode());
		}
		
		//final ArrayList<Items> list = ArrayList;
		
		productList.setAdapter(new ProductAdapter(getActivity(), ArrayList));

		productList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				//MainActivity activity = (MainActivity) getActivity();
				items = ArrayList.get(position);
				//activity.selectedDebtor = debtor;
				//activity.cusPosition=position;
				et_item.setText(items.getFITEM_ITEM_NAME());
				et_unit.setText(items.getFITEM_NOU_CASE());
				
				double unitPrice= Double.parseDouble(priDS.getProductPriceByCode(items.getFITEM_ITEM_CODE()))/Double.parseDouble(items.getFITEM_NOU_CASE());
				//int unitPrice=priDS.getProductPriceByCode(items.getFITEM_ITEM_CODE())/Integer.parseInt(items.getFITEM_NOU_CASE());
				Log.v("unit price", unitPrice+"");
				et_price.setText(String.format("%.2f", unitPrice));
				//Toast.makeText(getActivity(), items.getFITEM_ITEM_NAME() + " selected", Toast.LENGTH_SHORT).show();
				
				
				toast.createToastMessage(items.getFITEM_ITEM_NAME(), getActivity());
				
				dialog.dismiss();
			}
		});
		
			
		search.setOnQueryTextListener(new OnQueryTextListener() {
			
			@Override
			public boolean onQueryTextSubmit(String query) {
				return false;
			}
			
			@Override
			public boolean onQueryTextChange(String newText) {
				ItemsDS itemsDS =new ItemsDS(getActivity());
				productList.clearTextFilter();
				//final ArrayList<Items> list= itemsDS.getAllItemForVanSale(newText,"txntype !='22'");
				String currentRoute = spTranType.getSelectedItem().toString();
				//ArrayList<Items> ArrayList = null;
				ArrayList.clear();
				if(currentRoute.equals("MR - Market Return")){
					ArrayList= itemsDS.getAllItemForVanSale(newText,"txntype ='31'",referenceNum.getCurrentRefNo(getResources().getString(R.string.VanNumVal)),new SalRepDS(getActivity()).getCurrentRepCode());
				}
				else if(currentRoute.equals("UR - Re Usable Return")){
					ArrayList= itemsDS.getAllItemForVanSale(newText,"txntype ='25'",referenceNum.getCurrentRefNo(getResources().getString(R.string.VanNumVal)),new SalRepDS(getActivity()).getCurrentRepCode());
				}
				else if(currentRoute.equals("FD - Free Issues")){
					ArrayList= itemsDS.getAllItemForVanSale(newText,"txntype ='27'",referenceNum.getCurrentRefNo(getResources().getString(R.string.VanNumVal)),new SalRepDS(getActivity()).getCurrentRepCode());
				}
				productList.setAdapter(new ProductAdapter(getActivity(), ArrayList));
				return false;
			}
		});				
		dialog.show();
	}
	
	private void clearTextFields() {
		// TODO Auto-generated method stub
		index=0;
		
		et_item.setText("");
		et_unit.setText("0");
		et_price.setText("0.00");
		et_cases.setText("0");
		et_pieces.setText("0");
		
		btnAdd.setText("Add");
		
	}
	
	private void setTitle(String title){
		SetTitleAdapter adapter =new SetTitleAdapter();
		adapter.createCutomActionBarTitle(title, getActivity());
	}
}
